import { HemisphericLight, Engine, FreeCamera, Vector3, Mesh, Scene } from 'babylonjs'


export class BEngine {
  private dom: HTMLCanvasElement
  private engine: Engine
  private scene: Scene
  private camera: FreeCamera

  constructor(dom: HTMLCanvasElement) {
    this.dom = dom
    this.engine = new Engine(this.dom, true)  // 设置引擎
    this.scene = new Scene(this.engine)   // 创建场景
    this.camera = new FreeCamera('camera', new Vector3(9, 5, -10), this.scene)  // 创建相机
    this.camera.setTarget(Vector3.Zero())  // 设置相机起点

    this.camera.attachControl(this.dom, false)  // 把相机放到画布

    const light = new HemisphericLight('light', new Vector3(0, 1, 0), this.scene);  // 放置光源

    const sphere = Mesh.CreateSphere('sphere', 16, 2, this.scene)  // 放置球  名称 宽度、深度、场景
    sphere.position.y = 1

    const ground = Mesh.CreateGround('ground', 6, 6, 2, this.scene)  // 创建地板 名称 宽度、深度、场景

    window.addEventListener('resize', () => {
      this.engine.resize();
    })
  }

  render() {
    // 引擎渲染场景
    this.engine.runRenderLoop(() => {
      this.scene.render()
    })
  }




}


